package com.example.mcodebox;

import com.example.bean.Login;
import com.example.database.DBOpenHelper;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class TabHostMainActivity extends TabActivity {
	public static final int showTabSpec = 1;
	public static final int addTabSpec = 0;

	public static final int deleteTabSpec = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_tab_host_main);
		createTabHost();
		validatePassin();
	}

	private void validatePassin() {
		String passin = getpassinInfo();
		System.out.println(passin + "<-----------passin");
		if (passin != null) {
			createDialog(passin);
		} else {
			createLoginDialog();
		}
	}

	private String getpassinInfo() {// 从数据库中获取已经注册的数据库中的登录密码
		DBOpenHelper dbOpenHelper = new DBOpenHelper(getApplicationContext());

		String password = dbOpenHelper.queryPassword();
		System.out.println("获取登录到数据库的密码" + password);
		return password;
	}

	// 创建首次登录的密码登录对话框

	private void createLoginDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				TabHostMainActivity.this);

		LayoutInflater factory = LayoutInflater.from(this);
		final View view = factory.inflate(R.layout.first_login, null);
		builder.setTitle("这是你登录应用的唯一凭证。。。");
		builder.setView(view);
		final EditText editText = (EditText) view
				.findViewById(R.id.first_login_Edt);
		final EditText editText2 = (EditText) view
				.findViewById(R.id.first_login_again_Edt);

		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				final String loginAgain = editText2.getText().toString();
				final String login = editText.getText().toString();
				System.out.println("注册的密码1" + login);
				System.out.println("注册的密码2" + loginAgain);
				if (login.length() < 6) {
					createLoginDialog();
					Toast.makeText(TabHostMainActivity.this, "密码不能少于6位。。。",
							Toast.LENGTH_SHORT).show();
				} else {

					if (login.equals(loginAgain) == false) {
						createLoginDialog();
						Toast.makeText(TabHostMainActivity.this,
								"两次密码输入不正确，请重新输入。。。", Toast.LENGTH_SHORT)
								.show();

					}
					// 将数据插入到数据库中
					ContentValues values = new ContentValues();
					values.put("password", login);
					// 实例化数据库类
					DBOpenHelper helper = new DBOpenHelper(
							getApplicationContext());
					helper.insert2(values);
					System.out.println("插入到Login中！");
					Toast.makeText(TabHostMainActivity.this, "插入成功！",
							Toast.LENGTH_SHORT).show();

				}

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				System.exit(CONTEXT_INCLUDE_CODE);
			}
		});

		// 这是什么意思啊。。。不知道
		/*
		 * 这是当这个对话框出现时，点击手机上的返回键没有用！
		 */
		builder.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH
						|| keyCode == KeyEvent.KEYCODE_BACK
						|| keyCode == KeyEvent.KEYCODE_ESCAPE) {
					return true;
				} else {
					return false;
				}
			}
		});
		builder.create().show();
	}

	// 不是第一次进入到软件时，弹出的登录验证
	private void createDialog(String passin) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				TabHostMainActivity.this);
		LayoutInflater factory = LayoutInflater.from(this);

		final View view = factory.inflate(R.layout.login, null);
		final String password = passin;

		builder.setTitle("请输入登录密码：");
		builder.setView(view);

		builder.setPositiveButton("确认", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				EditText editText = (EditText) view
						.findViewById(R.id.login_Edt);
				String temp = editText.getText().toString();
				System.out.println(temp + "输入到登录框的密码值！");
				if (temp.equals(password)) {
					Toast.makeText(TabHostMainActivity.this, "密码正确！",
							Toast.LENGTH_SHORT).show();
				} else {
					createDialog(password);
					Toast.makeText(TabHostMainActivity.this, "密码不正确!",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				System.exit(CONTEXT_INCLUDE_CODE);
			}
		});

		builder.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK
						|| keyCode == KeyEvent.KEYCODE_SEARCH) {
					return true;
				} else {

					return false;
				}
			}
		});
		builder.create().show();
	}

	public void createTabHost() {
		TabHost tabhost = getTabHost();// tabhost继承tabactivity

		Intent addIntent = new Intent();
		addIntent.setClass(this, AddActivity.class);
		TabHost.TabSpec addTabSpec = tabhost.newTabSpec("Add");
		addTabSpec.setIndicator("Add");
		addTabSpec.setContent(addIntent);
		tabhost.addTab(addTabSpec);
		tabhost.setCurrentTab(0);// 设置进入到应用时显示的界面

		Intent showIntent = new Intent();// 使用intent使showactivity与tabhostactivity之间的链接
		showIntent.setClass(this, ShowActivity.class);

		TabHost.TabSpec showTabSpec = tabhost.newTabSpec("Show");// 创建一个新的tab空间

		showTabSpec.setIndicator("Show");// 为标签添加特定的标签
		showTabSpec.setContent(showIntent);// 指定一个意图使用来启动一个活动作为标签内容。

		tabhost.addTab(showTabSpec);// 在tabhost上添加一个标签

		Intent deleteIntent = new Intent();
		deleteIntent.setClass(this, DeleteActivity.class);

		TabHost.TabSpec deleteSTabSpec = tabhost.newTabSpec("Delete");
		deleteSTabSpec.setIndicator("Delete");
		deleteSTabSpec.setContent(deleteIntent);
		tabhost.addTab(deleteSTabSpec);

		// 当tabhost里面的space变化时，产生相应的事件
		tabhost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {

				Toast.makeText(TabHostMainActivity.this, "This is a tabhost !",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tab_host_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_tab_host_main,
					container, false);
			return rootView;
		}
	}

}
